package com.gateway.plugin.base.util;

import com.gateway.plugin.api.RemoteAddressResolver;
import com.gateway.plugin.api.util.SpringBeanUtils;
import org.springframework.web.server.ServerWebExchange;

/**
 * @program: ahcloud-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2024/6/8 10:13
 **/
public class HostAddressUtils {

    private HostAddressUtils() {
    }

    /**
     * Acquire host string.
     *
     * @param exchange the exchange
     * @return the string
     */
    public static String acquireHost(final ServerWebExchange exchange) {
        return SpringBeanUtils.getInstance().getBean(RemoteAddressResolver.class).resolve(exchange).getHostString();
    }


    /**
     * 获取ip字符串
     * @param exchange
     * @return
     */
    public static String acquireIp(final ServerWebExchange exchange) {
        return SpringBeanUtils.getInstance().getBean(RemoteAddressResolver.class).resolve(exchange).getAddress().getHostAddress();
    }
}
